const log4js = require('koa-log4');
const logger = log4js.getLogger('services::site');
const bson = require('bson');
const DBRef = bson.DBRef;
const mongodb = require('mongodb');
const ObjectId = mongodb.ObjectId;
const helper = require('mongoskin').helper;
const db = require('../mongodb');

/**
 * 查找所有站点
 * @returns {Promise}
 */
const findAll = async () => {
    return new Promise((resolve, reject) => {
        db.site.find({}, {admin: 0}).toArray(async (err, result) => {
            if (!err) {
                resolve(result);
            } else {
                reject(err);
            }
        });
    });
};

const findByPinyin = async (pinyin) => {
    return new Promise((resolve, reject) => {
        db.site.findOne({subDomain: pinyin}, async (err, result) => {
            if (!err) {
                resolve(result);
            } else {
                reject(err);
            }
        });
    });
};
/**
 * 查找所有站点
 * @returns {Promise}
 */
const find = async (query, fields) => {
    return new Promise((resolve, reject) => {
        db.site.find(query, fields).toArray(async (err, result) => {
            if (!err) {
                resolve(result);
            } else {
                reject(err);
            }
        });
    });
};


/**
 * 根据站点id查找站点
 * @param id
 * @returns {Promise}
 */
const findById = async (id, fields) => {
    // fields = fields || {admin: 1};
    return new Promise((resolve, reject) => {
        db.site.findOne({_id: helper.toObjectID(id)}, fields, async (err, result) => {
            if (!err) {
                resolve(result);
            } else {
                reject(err);
            }
        });
    });
};

/**
 * 添加站点
 * @param site
 * @returns {Promise}
 */
const insert = async (site) => {
    // 处理city字段
    site.city = new DBRef('city', helper.toObjectID(site.city));
    return new Promise((resolve, reject) => {
        db.site.insert(site, async (err, result) => {
            if (!err) {
                resolve(result);
            } else {
                reject(err);
            }
        });
    });
};

/**
 * 保存站点
 * @param site
 * @returns {Promise}
 */
const save = async (site) => {
    site.city = new DBRef('city', helper.toObjectID(site.city));
    return new Promise((resolve, reject) => {
        db.site.save(site, async (err, result) => {
            if (!err) {
                resolve(result);
            } else {
                reject(err);
            }
        });
    });
};


/**
 *
 * @param update
 * @returns {Promise}
 */
const update = async (query, update) => {
    return new Promise((resolve, reject) => {
        // update({查询字段},{修改部分},function(error,result){ //异步回掉函数 });
        db.site.update(query, update, async (err, result) => {
            if (!err) {
                resolve(result);
            } else {
                reject(err);
            }
        });
    });
};

const findAndModify = async (query, update,) => {
    return new Promise((resolve, reject) => {
        // findAndModify({查询字段},[排序字段],{修改部分},{配置属性},function(error,result){ //异步回掉函数} });
        db.site.findAndModify(query, {}, update, {new: true, upsert: true}, async (err, result) => {
            if (!err) {
                resolve(result);
            } else {
                reject(err);
            }
        });
    });
};


/**
 * 根据id删除信息
 * @param id
 * @returns {Promise}
 */
const delById = async (id) => {
    return new Promise(function (resolve, reject) {
        db.site.remove({_id: helper.toObjectID(id)}, async (err, result) => {
            if (!err) {
                resolve(result);
            } else {
                reject(err);
            }
        });
    });
};


exports = module.exports = {
    findAll, findById, find, insert, update, save, findAndModify, findByPinyin, delById
};
